package net.jgrm.order.controller;

import com.alibaba.fastjson.JSON;
import net.jgrm.idgenerator.service.impl.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: houyong
 * @date: 2025年09月01日 14:04
 */
@RestController
@RequestMapping("/id-generator")
public class IdGeneratorController {


    @Autowired
    private IdGeneratorService idGeneratorService;


    @RequestMapping("/generate")
    public Long generateId(@RequestParam String key) {
        return idGeneratorService.generateId(key);
    }

    @RequestMapping("batch/generate")
    public Map batchGenerateId(@RequestParam String key, @RequestParam Integer count) {
        Long[] ids = idGeneratorService.batchGenerateId(key, count);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("count", count);
        result.put("ids", JSON.toJSONString(ids));

        // 将Long[]转换为String[]以避免JavaScript对大整数的处理问题
        String[] idsStr = new String[ids.length];
        for (int i = 0; i < ids.length; i++) {
            idsStr[i] = ids[i].toString();
        }
        result.put("ids2", idsStr);
        result.put("ids3", ids);
        result.put("message", "使用IdGeneratorService批量生成ID成功");
        return result;
    }

}
